/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package codegener.tool;

import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author 32181
 */
public class Gener {
    
    private final static Configuration configuration = new Configuration(Configuration.getVersion());
    private final static StringTemplateLoader templateLoader=new StringTemplateLoader();
    private final static Map<String,String> temps=new HashMap<>();
    static{
		
	    try {
                     File dir=new File("./ftl");
                     File[] ftsFiles=dir.listFiles();
                     if(null!=ftsFiles&&ftsFiles.length>0){
                         temps.clear();
                         for(File ft:ftsFiles){
                             String content=FileUtils.readFileToString(ft, "UTF-8");
                             temps.put(ft.getName(), content);
                         }
                     }            
                     
			    configuration.setDefaultEncoding("UTF-8");
			    configuration.setLogTemplateExceptions(false);
			    configuration.setTemplateLoader(templateLoader);
			    
	        } catch (Exception e) {
			e.printStackTrace();
		}
	}
 
    public static Map<String,String> getTemps(){
        return temps;
    }
    
    public static void flushTemplate(Map<String,String> temps){
        temps.forEach((name, temp) -> {
                templateLoader.putTemplate(name, temp);
        });
    }
    
    public static void generBean(Map<String,Object> data) {
		try {
			File f=new File(String.valueOf(data.get("beanFileName")));
			f.getParentFile().mkdirs();
			FileOutputStream fout=new FileOutputStream(f);
			Template temp = configuration.getTemplate("Bean.ftl","UTF-8");
			Writer out = new OutputStreamWriter(fout,"UTF-8");
			temp.process(data, out);
			out.close();
			fout.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
    /**
	 * 生成Mapper
	 * @param tab
	 */
	public static void generMapper(Map<String,Object> data){
		try {
			File f=new File(String.valueOf(data.get("mapperFileName")));
			f.getParentFile().mkdirs();
			FileOutputStream fout=new FileOutputStream(f);
			Template temp = configuration.getTemplate("Mapper.ftl","UTF-8");
			Writer out = new OutputStreamWriter(fout,"UTF-8");
			temp.process(data, out);
			out.close();
			fout.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 生成XMLMapper
	 * @param tab
	 */
	public static void generXMLMapper(Map<String,Object> data){
		try {
			File f=new File(String.valueOf(data.get("xmlmapperFileName")));
			f.getParentFile().mkdirs();
			FileOutputStream fout=new FileOutputStream(f);
			Template temp = configuration.getTemplate("XMLMapper.ftl","UTF-8");
			Writer out = new OutputStreamWriter(fout,"UTF-8");
			temp.process(data, out);
			out.close();
			fout.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 生成Service
	 * @param tab
	 */
	public static void generService(Map<String,Object> data){
		try {
			File f=new File(String.valueOf(data.get("serviceFileName")));
			f.getParentFile().mkdirs();
			FileOutputStream fout=new FileOutputStream(f);
			Template temp = configuration.getTemplate("Service.ftl","UTF-8");
			Writer out = new OutputStreamWriter(fout,"UTF-8");
			temp.process(data, out);
			out.close();
			fout.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 生成Service
	 * @param tab
	 */
	public static void generAction(Map<String,Object> data){
		try {
			File f=new File(String.valueOf(data.get("actionFileName")));
			
			f.getParentFile().mkdirs();
			FileOutputStream fout=new FileOutputStream(f);
			Template temp = configuration.getTemplate("Action.ftl","UTF-8");
			Writer out = new OutputStreamWriter(fout,"UTF-8");
			temp.process(data, out);
			out.close();
			fout.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
    
}
